﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SocialShare.BLL;
using SocialShare.BusinessEntities;
using System.Configuration;
using SocialShare.DAL.Facades;

namespace SocialShare.Web.Admin
{
    public partial class edit_user : System.Web.UI.Page
    {
        private Int32 UserID
        {
            get
            {
                try
                {
                    if (HttpContext.Current.Request.QueryString["id"] != null && !String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["id"]))
                    {
                        return Convert.ToInt32(HttpContext.Current.Request.QueryString["id"]);
                    }
                    else
                    {
                        return -1;
                    }
                }
                catch
                {
                    return -1;
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            if (UserID != -1)
            {
                MemberFacade memberFacade = new MemberFacade();
                SocialShare_Members member=memberFacade.GetMemberByMember_Id(UserID);
                txtEmailAddress.Text=member.Email;
                txtScreenName.Text=member.ScreenName;
                txtPassword.Text=member.Password;

                SocialShare_MemberProfiles profile = memberFacade.GetUserProfileByID(UserID.ToString());
                if (profile != null)
                {
                    txtBlogName.Text = profile.BlogName;
                    txtDOB.Text = profile.DOB.Value.ToShortDateString();
                    txtFirstName.Text = profile.First_Name;
                    txtlastName.Text = profile.Last_Name;
                    txtWebSite.Text = profile.Website;
                    rdblSex.SelectedIndex = rdblSex.Items.IndexOf(rdblSex.Items.FindByValue(profile.Sex));
                }
            }
        }
        protected void lnbSave_Click(object sender, EventArgs e)
        {
            bool isEmailChanged = false;
            bool isScreenNameChanged = false;

            ConfigReader configReader = new ConfigReader();
            MemberFacade memberFacade = new MemberFacade();

            SocialShare_Members member = memberFacade.GetMemberByMember_Id(UserID);
            if (member.Email.ToLower() != txtEmailAddress.Text.Trim().ToLower()) { isEmailChanged = true; }
            member.Email = txtEmailAddress.Text.Trim();

            if (member.ScreenName.ToLower() != txtScreenName.Text.Trim().ToLower()) { isScreenNameChanged = true; }
            member.ScreenName = txtScreenName.Text.Trim();
            member.Password = txtPassword.Text.Trim();

            SocialShare_MemberProfiles profile = memberFacade.GetUserProfileByID(UserID.ToString());
            if (profile == null)
            {
               profile = new SocialShare_MemberProfiles();
            }
            profile.BlogName = txtBlogName.Text.Trim();
            profile.DOB = Convert.ToDateTime(txtDOB.Text.Trim());
            profile.First_Name = txtFirstName.Text.Trim();
            profile.Last_Name = txtlastName.Text.Trim();
            profile.Website = txtWebSite.Text.Trim();
            profile.Sex = rdblSex.SelectedItem.Value;

            if (isScreenNameChanged && memberFacade.GetMemberByScreenName(txtScreenName.Text.Trim()) != null)
            {
                divMessage.Attributes.Add("class", "error");
                lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.SCREEN_NAME_EXISTS.ToString());

            }

            if (isEmailChanged && memberFacade.GetMemberByUserName(txtEmailAddress.Text.Trim()) != null)
            {
                divMessage.Attributes.Add("class", "error");
                lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.EMAIL_EXISTS.ToString());

            }
            else
            {
                try
                {
                    memberFacade.UpdateMember(member);
                    if (profile.ProfileId > 0)
                    {
                        //Existing user profile
                        memberFacade.UpdateMemberProfile(profile);
                    }
                    else
                    {
                        //New user profile
                        profile.UserId = member.Member_Id.ToString();
                        memberFacade.CreateNewMemberProfile(profile);
                    }

                    divMessage.Attributes.Add("class", "success");
                    lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.REGISTRATION_SUCCESS.ToString());

                }
                catch
                {
                    divMessage.Attributes.Add("class", "error");
                    lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.REGISTRATION_FAILED.ToString());
                }
            }
        }
    }
}